package com.ydd.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydd.dto.LinkDTO;
import com.ydd.entity.Link;
import com.ydd.result.Result;
import com.ydd.service.ILinkService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@RestController
@RequestMapping("/admin/ydd_link")
public class LinkController {
    @Autowired
    private ILinkService iLinkService;

    @GetMapping("/list")
    public Result<Map<String, Object>> linkList(LinkDTO linkDTO){
        Page<Link> linkPage = new Page<>(linkDTO.getPage(),linkDTO.getLimit());
        //List<Link> page = iLinkService.getPage(linkDTO);
        // 条件分页查询
        LambdaQueryWrapper<Link> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.like(linkDTO.getType()!=null,Link::getType,linkDTO.getType());
        Page<Link> page = iLinkService.page(linkPage, queryWrapper);
        List<Link> linkList = linkPage.getRecords();
        //IPage<List<Link>> iPage = iLinkService;
        Map<String,Object> map = new HashMap<>();
        map.put("count",page.getTotal());
        map.put("currentPage",page.getCurrent());
        map.put("data",linkList);
        map.put("pageSize",linkDTO.getLimit());
        map.put("totalPage",page.getPages());

        return Result.success(map);

    }
    //@GetMapping("/list")
    //public Result<Page> linkList(LinkDTO linkDTO){
    //
    //    Page<Link> linkPage = new Page<>(linkDTO.getPage(),linkDTO.getLimit());
    //
    //    LambdaQueryWrapper<Link> queryWrapper =
    //            new LambdaQueryWrapper<>();
    //    queryWrapper.like(linkDTO.getType()!=null,Link::getType,linkDTO.getType());
    //
    //    Page<Link> page = iLinkService.page(linkPage, queryWrapper);
    //
    //
    //
    //    long current = page.getCurrent();
    //    long size = page.getSize();
    //    long total = page.getTotal();
    //    System.out.println("size:"+ size +"total:"+total+"  current:"+current);
    //    //IPage<>
    //
    //    return Result.success(page);
    //}
    @GetMapping("/rowInfo")
    public Result<Link> rowInfo(@RequestParam Integer id){

        Link link = iLinkService.rowInfo(id);
        return Result.success(link);
    }
    @PostMapping("/update")
    public Result upd(@RequestBody Link link){
        iLinkService.upd(link);
        return Result.success();
    }
    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        iLinkService.del(id);
        return Result.success();
    }
    @PostMapping("/add")
    public Result add(@RequestBody Link link){
        iLinkService.add(link);
        return Result.success();
    }

}
